package com.ad.pet.common.data.injector.module;

import android.support.annotation.NonNull;

import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.google.gson.GsonBuilder;
import com.ad.pet.App;
import com.ad.pet.api.ApiService;
import com.ad.pet.common.data.http.interceptor.LoggerInterceptor;
import com.ad.pet.common.data.utils.JsonUtil;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by sll on 2015/3/7.
 */
@Module
public class ApiModule {

    @Provides
    @Singleton
    public ApiService provideApi(Retrofit retrofit, JsonUtil jsonUtil) {
        return new ApiService(retrofit, jsonUtil);
    }

//    @Provides
//    @Singleton
//    public DispatchApiService provideDispatchApi(ResourcesUtil resourcesUtil) {
//        String ip = resourcesUtil.getString(R.string.dispatch_ip);
//        HttpsUtils.SSLParams sslParams = null;
//        try {
//            InputStream serverCer = new Buffer().writeUtf8(HttpHelp.serverCerStr).inputStream();
//            InputStream clientBks = App.getApp().getAssets().open("client.bks");
//            //初始化https证书参数信息
//            sslParams = HttpsUtils.getSslSocketFactory(new InputStream[]{serverCer}, clientBks, "im_cms");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        OkHttpClient okHttp = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)
//                .writeTimeout(15, TimeUnit.SECONDS).readTimeout(15, TimeUnit.SECONDS)
//                .addInterceptor(new LoggerInterceptor("OkHttp", true))
//                .hostnameVerifier(new HostnameVerifier() {
//                    @Override
//                    public boolean verify(String hostname, SSLSession session) {
//                        return true;
//                    }
//                })
//                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
//                .build();
//
//        Retrofit retrofit = getRetrofit(okHttp, ip);
//        DispatchApi api = retrofit.create(DispatchApi.class);
//        return new DispatchApiService(api);
//    }



    @NonNull
    private Retrofit getRetrofit(OkHttpClient okHttpClient, String ip) {
        return new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create()))
                .baseUrl(ip)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    @NonNull
    private OkHttpClient getDefaultOkHttpClient() {
        return new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS).readTimeout(15, TimeUnit.SECONDS).cookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(App.getApp())))
                .addInterceptor(new LoggerInterceptor("OkHttp", true))
                .build();
    }
}
